/*
 * TaskEdit.java
 *
 * Created on 23.06.2008, 14:23
 *
 * Copyright (c) 2006-2008, Daniel Apatin (ad), http://apatin.net.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * You can also redistribute and/or modify this program under the
 * terms of the Psi License, specified in the accompanied COPYING
 * file, as published by the Psi Project; either dated January 1st,
 * 2005, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package Tasks;

import Menu.TaskEditMenu;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.TextField;
import locale.SR;
import midlet.StaticData;
import ui.controls.form.DefForm;
import ui.controls.form.DropChoiceBox;
import ui.controls.form.SimpleString;
import ui.controls.form.TextInput;

/**
 *
 * @author ad
 */
public class TaskEdit extends DefForm {
    
    TaskItem task;
    boolean newTask=false;

    private TaskList tasklist;
    
    private TextInput fromPath;
    private TextInput toPath;
    
    private DropChoiceBox typeBox;
    
    /** Creates a new instance of TaskEdit */
    public TaskEdit(Display display, TaskItem task, TaskList tasklist) {
        super(display, "");
        
        this.display=display;
        parentView=display.getCurrent();
        
        this.tasklist=tasklist;
        
        newTask=(task==null);
        this.task=(newTask)?new TaskItem("", "", 0, ""):task;
        
	fromPath = new TextInput(display, "From", (newTask)?"":task.getFrom(), null, TextField.ANY);//128, TextField.ANY
        itemsList.addElement(fromPath);
        
	toPath = new TextInput(display, "To", (newTask)?"":task.getTo(), null, TextField.ANY);//128, TextField.ANY
        itemsList.addElement(toPath);
        
        itemsList.addElement(new SimpleString("Type", true));
        typeBox=new DropChoiceBox(display);
        typeBox.append("copy"); 
        typeBox.append("delete"); 
        typeBox.append("purgedir"); 
        typeBox.append("chmod"); 
        typeBox.append("link"); 
        typeBox.append("move"); 
        typeBox.append("rename"); 
        typeBox.append("cleardir"); 

        typeBox.setSelectedIndex((newTask)?0:task.getType());
        itemsList.addElement(typeBox);
        
        moveCursorTo(getNextSelectableRef(-1));
        attachDisplay(display);
        
    }
    
    public void cmdOk() {
        task.setType(typeBox.getValue());
        task.setSource(fromPath.getValue());
        task.setDest(toPath.getValue());

        if (newTask)
            tasklist.tasks.addElement(task);
        
        destroyView();
    }
    
    public void setSource(String from) {
        fromPath.setValue(from);
    }
    
    public void setDest(String to) {
        toPath.setValue(to);
    }
    
    public String getLeftCommand() { return SR.MS_MENU; }
    public void leftCommand() { new TaskEditMenu(display, this, (cursor==1), (cursor==0)); }
    
    public String getCenterCommand() { return SR.MS_OK; }
    public void centerCommand() { cmdOk(); }
        

    public String getRightCommand() { return SR.MS_CANCEL; }
    public void rightCommand() { destroyView(); }
    
    public void destroyView()	{
        if (display!=null) display.setCurrent(StaticData.getInstance().tasklist);
    }

    public String getSource() {
        return fromPath.getValue();
    }

    public String getDest() {
        return toPath.getValue();
    }
}
